/**
* Author: Bob Chen
*/

package net.uman.framework.modules.objectpool;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import net.uman.entity.IdEntity;
import net.uman.framework.modules.utils.ApplicationContextProvider;
import net.uman.service.Manager;

public class Action {    
    protected String getModelName(IdEntity owner) {
    	String s = owner.getClass().getSimpleName();
    	int i = s.indexOf("_$$_");
    	if (i > 0) {
    		s = s.substring(0, i);
    	}
    	return s;
    }
    
    protected IdEntity getObject(String modelName, Long id) {
//        Manager manager = getManager(modelName);
//        String methodName = "get"+firstUpperCase(modelName);
//        try {
//            Method method = manager.getClass().getMethod(methodName, new Class[] {long.class});
//            if (method == null) {
//                System.out.println("Method not found: "+methodName);
//            }
//            IdEntity model = (IdEntity)method.invoke(manager, id);
//            return model;
//        } catch (SecurityException e) {
//            e.printStackTrace();
//        } catch (NoSuchMethodException e) {
//            e.printStackTrace();
//        } catch (IllegalAccessException e) {
//            e.printStackTrace();
//        } catch (InvocationTargetException e) {
//            e.printStackTrace();
//        }
//        
//        return null;
    	String cls = "net.b2bcenter.entity." + firstUpperCase(modelName);
    	try {
			return (IdEntity)Class.forName(cls).newInstance();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return null;
    }
    
    protected Object getBeanProperty(IdEntity obj, String field) {
//        try {
//            Method method = getMethod(obj.getClass(), field);
//            
//            if (method == null) {
////                throw new RuntimeException("Read method not found: " + field);
//            	return null;
//            }
//            
//            return method.invoke(obj);
//        } catch (IllegalAccessException e) {
//            e.printStackTrace();
//        } catch (InvocationTargetException e) {
//            e.printStackTrace();
//        }
    	
    	try {
    		if ("id".equals(field)) {
    			return obj.getId();
    		}
    		
    		Field fld = null;
    		try {
    			fld = obj.getClass().getDeclaredField(field);
    		} catch (NoSuchFieldException e) {
    			if (obj.getClass().getSuperclass() != null) {
    				fld = obj.getClass().getSuperclass().getDeclaredField(field);
    			}
    		}
    		
    		fld.setAccessible(true);
			return fld.get(obj);
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			e.printStackTrace();
		}
        return null;
    }
    
    private Method getMethod(Class cls, String field) {
    	try {
            return cls.getMethod("get" + firstUpperCase(field), new Class[0]);
        } catch (Exception e) {
        	try {
        		return cls.getMethod("is" + firstUpperCase(field), new Class[0]);
        	} catch (Exception e1) {
            	try {
            		return cls.getMethod("get" + field, new Class[0]);
            	} catch (Exception e2) {
            		try {
            			return cls.getMethod("is" + field, new Class[0]);
            		} catch (Exception e3) {
            			return null;
            		}
            	}
            }
        }
    }
    
    protected String firstUpperCase(String s) {
        if (s == null || s.length() < 2) {
            return s;
        }
        return s.substring(0, 1).toUpperCase() + s.substring(1);
    }
    
    protected Manager getManager(String modelName) {
    	String managerName = modelName.substring(0, 1).toLowerCase()+modelName.substring(1)+"Manager";
    	Manager manager = (Manager)ApplicationContextProvider.getApplicationContext().getBean(managerName);
        return manager;
    }
    
    protected IdEntity createObject(String modelName) {
        try {
        	IdEntity model = (IdEntity)Class.forName("net.b2bcenter.entity."+modelName).newInstance();
            return model;
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        
        return null;
    }
}
